package com.zhouchao.web;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhouchao.bean.Dorm;
import com.zhouchao.bean.DormManager;
import com.zhouchao.bean.Score;
import com.zhouchao.dao.DormBuildDao;
import com.zhouchao.dao.DormDao;
import com.zhouchao.dao.ScoreDao;
import com.zhouchao.util.DbUtil;
import com.zhouchao.util.TimeUtil;

public class ScoreServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		String action = request.getParameter("action");
		HttpSession session = request.getSession();
		Object currentUserType = session.getAttribute("currentUserType");
		DormDao dormDao = new DormDao();
		ScoreDao sd=new ScoreDao();
		Score score=new Score();
		DbUtil dbUtil = new DbUtil();
		TimeUtil time = new TimeUtil();
		String tm = time.Time();
		
		Connection con = null;
		if("stulist".equals(action)){
			
			try {
				con=dbUtil.getCon();
				
				List<Score> scorelist=sd.scoreList(con);
				request.setAttribute("scorelist", scorelist);
				request.setAttribute("mainPage", "student/score.jsp");
				request.getRequestDispatcher("mainStudent.jsp").forward(request,response);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			
			
			
			
			
		}else if("list".equals(action)){
		
			try {
				con=dbUtil.getCon();
				
				List<Score> scorelist=sd.scoreList(con);
				request.setAttribute("scorelist", scorelist);
				request.setAttribute("mainPage", "dormManager/scorelist.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request,response);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
		}else if("search".equals(action)){
			try {
				con=dbUtil.getCon();
				List<Score> list=null;
				int week =Integer.parseInt( request.getParameter("week"));
				String dormid = request.getParameter("dormid");
				if(week==0&&"all".equals(dormid)){
					list=sd.scoreList(con);
				}else if("all".equals(dormid)&&week!=0){
					list=sd.scoreList(con, week);
				}else{
					list=sd.scoreList(con, week,dormid);
				}
				
				if("dormManager".equals((String)currentUserType)) {
				request.setAttribute("scorelist",list);
				request.setAttribute("mainPage", "dormManager/scorelist.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request,response);
				}
				else if ("student".equals((String)currentUserType)){
					request.setAttribute("scorelist",list);
					request.setAttribute("mainPage", "student/scorelist.jsp");
					request.getRequestDispatcher("mainStudent.jsp").forward(request,response);
					
				}
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		else if("set".equals(action)){
			DormManager manager = (DormManager)(session.getAttribute("currentUser"));
			int buildId = manager.getDormBuildId();
			try {
				
				con=dbUtil.getCon();
				
				
				String buildName = DormBuildDao.dormBuildName(con, buildId);
				List<Dorm> dormlist = dormDao.dormList(con, buildId);
				request.setAttribute("buildName", buildName);
				request.setAttribute("buildId", buildId);
				request.setAttribute("dormlist", dormlist);
				request.setAttribute("time", tm);
				request.setAttribute("mainPage", "dormManager/score.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request,response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}else{
			try {
				DormManager manager = (DormManager)(session.getAttribute("currentUser"));
				int buildId = manager.getDormBuildId();
				con=dbUtil.getCon();
				
				String buildid = request.getParameter("buildid");
				String dorm = request.getParameter("dorm");
				int week =Integer.parseInt( request.getParameter("week"));
				String date = request.getParameter("date");
				Float sum = Float.parseFloat(request.getParameter("sum"));
				 score=new Score(buildid,dorm,date,week,sum);
				sd.intoscore(con, score);
				
				String buildName = DormBuildDao.dormBuildName(con, buildId);
				List<Dorm> dormlist = dormDao.dormList(con, buildId);
				request.setAttribute("buildName", buildName);
				request.setAttribute("dormlist", dormlist);
				request.setAttribute("time", tm);
				request.setAttribute("mainPage", "dormManager/score.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request,response);
				
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
	}

}
